/*
 * Copyright 2009 Max Ishchenko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.ishchenko.idea.nginx.configurator;

import com.intellij.openapi.ui.ComponentWithBrowseButton;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.CellConstraints;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Max
 * Date: 28.07.2009
 * Time: 3:41:46
 */
public class ServerFieldsForm {
    private JPanel panel;
    private JTextField nameField;
    private ComponentWithBrowseButton<JTextField> executableField;
    private ComponentWithBrowseButton<JTextField> configurationField;
    private ComponentWithBrowseButton<JTextField> pidField;
    private JTextField globalsField;


    public ServerFieldsForm(final NginxConfigurationPanel.TrickyMediator mediator) {

        $$$setupUI$$$();
        KeyAdapter syncListener = new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        mediator.sync();
                    }
                });
            }
        };

        nameField.addKeyListener(syncListener);
        executableField.getChildComponent().addKeyListener(syncListener);
        configurationField.getChildComponent().addKeyListener(syncListener);
        pidField.getChildComponent().addKeyListener(syncListener);
        globalsField.addKeyListener(syncListener);

        executableField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        mediator.chooseExecutableClicked();
                    }
                });
            }
        });

        configurationField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        mediator.chooseConfigurationClicked();
                    }
                });
            }
        });

        pidField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        mediator.choosePidClicked();
                    }
                });
            }
        });

        mediator.nameField = nameField;
        mediator.executableField = executableField.getChildComponent();
        mediator.configField = configurationField.getChildComponent();
        mediator.pidField = pidField.getChildComponent();
        mediator.globalsField = globalsField;

    }


    public JPanel getPanel() {
        return panel;
    }

    private void createUIComponents() {
        executableField = new ComponentWithBrowseButton<JTextField>(new JTextField(), null);
        configurationField = new ComponentWithBrowseButton<JTextField>(new JTextField(), null);
        pidField = new ComponentWithBrowseButton<JTextField>(new JTextField(), null);
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        panel = new JPanel();
        panel.setLayout(new FormLayout("fill:max(d;4px):noGrow,left:4dlu:noGrow,fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        nameField = new JTextField();
        nameField.setText("");
        CellConstraints cc = new CellConstraints();
        panel.add(nameField, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("net/ishchenko/idea/nginx/NginxBundle").getString("run.servername"));
        panel.add(label1, cc.xy(1, 1));
        final JLabel label2 = new JLabel();
        this.$$$loadLabelText$$$(label2, ResourceBundle.getBundle("net/ishchenko/idea/nginx/NginxBundle").getString("run.executable"));
        panel.add(label2, cc.xy(1, 3));
        panel.add(executableField, cc.xy(3, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        final JLabel label3 = new JLabel();
        this.$$$loadLabelText$$$(label3, ResourceBundle.getBundle("net/ishchenko/idea/nginx/NginxBundle").getString("run.configuration"));
        panel.add(label3, cc.xy(1, 5));
        panel.add(configurationField, cc.xy(3, 5, CellConstraints.FILL, CellConstraints.DEFAULT));
        final JLabel label4 = new JLabel();
        this.$$$loadLabelText$$$(label4, ResourceBundle.getBundle("net/ishchenko/idea/nginx/NginxBundle").getString("run.globals"));
        panel.add(label4, cc.xy(1, 9));
        globalsField = new JTextField();
        panel.add(globalsField, cc.xy(3, 9, CellConstraints.FILL, CellConstraints.DEFAULT));
        final JLabel label5 = new JLabel();
        this.$$$loadLabelText$$$(label5, ResourceBundle.getBundle("net/ishchenko/idea/nginx/NginxBundle").getString("run.pidpath"));
        panel.add(label5, cc.xy(1, 7));
        panel.add(pidField, cc.xy(3, 7, CellConstraints.FILL, CellConstraints.DEFAULT));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel;
    }
}
